package wwy.Service;

import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wwy.Common.LogicService;
import wwy.Dao.FoodsDao;
import wwy.Dto.FoodsDto;
import wwy.entity.Foods;
import wwy.entity.Merchant;

import java.util.ArrayList;
import java.util.List;

@Service
public class FoodsService extends LogicService<FoodsDao, Foods,Long> {
    private final MerchantService merchantService;
    public FoodsService(@Autowired FoodsDao dao, MerchantService merchantService) {
        super(dao); this.merchantService = merchantService;}
    @Transactional
    public List<FoodsDto> getFoodsByMerchantId(Long MerchantId){
        Merchant merchant = merchantService.getMerchantByMerchantId(MerchantId);
        List<Foods> foods = dao.findByMerchant(merchant);
        List<FoodsDto> foodsDtoS = new ArrayList<> ();
        for (Foods food : foods) {
            foodsDtoS.add(modelMapper.map(food,FoodsDto.class));
        }
        return foodsDtoS;
    }
    @Transactional
    public void saveFoods(List<Foods> foods){
        dao.saveAll(foods);
    }
    @Transactional
    public FoodsDto getFood(Long merchantId, String foodsName) {
        Merchant merchant = merchantService.getMerchantByMerchantId(merchantId);
        List<Foods> foods = dao.findByMerchant(merchant);
        FoodsDto findFood = null;
        for (Foods food : foods) {
            if (food.getName().equals(foodsName))
                findFood = modelMapper.map(food, FoodsDto.class);
        }
        return findFood;
    }
}
